/**
 * 
 */
package com.yunyike.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.EnumPromotionType;
import com.yunyike.dto.PromotionDTO;
import com.yunyike.dto.PromotionMerchandiseDTO;
import com.yunyike.exception.BusinessException;
import com.yunyike.mapper.PromotionMerchandiseMappingMapper;
import com.yunyike.model.Merchandise;
import com.yunyike.model.Promotion;
import com.yunyike.model.PromotionMerchandiseMapping;
import com.yunyike.model.Rule;
import com.yunyike.service.IMerchandiseService;
import com.yunyike.service.IPromotionMerchandiseMappingService;
import com.yunyike.service.IPromotionService;
import com.yunyike.service.IRuleService;

/**
 * @author zhengqiang.shi
 * 2016-3-31 下午5:44:01
 */
@Service("PromotionMerchandiseMappingServiceImpl")
public class PromotionMerchandiseMappingServiceImpl implements IPromotionMerchandiseMappingService {

	private static final Logger log = LoggerFactory.getLogger(PromotionMerchandiseMappingServiceImpl.class);
	
	@Autowired
	private PromotionMerchandiseMappingMapper promotionMerchandiseMappingMapper;

	@Autowired
	private IPromotionService promotionService;
	
	@Autowired
	private IRuleService ruleService;
	
	@Autowired
	private IMerchandiseService merchandiseService;
	
	public PromotionDTO findDefaultShowItemByPromotionId(String promotionId) {
		
		// 获取当前活动中的商品id
		List<PromotionMerchandiseMapping> list = promotionMerchandiseMappingMapper.selectByPromotionId(promotionId);
		if(list == null || list.isEmpty()){
			log.info("selectByPromotionId return null, please check db,promotionId:{}",promotionId);
			return null;
		}
		
		// 获取默认显示的商品id
		List<String> defaultShowItemId = findItemId(list,Arrays.asList(CommonConstant.NUMBER_1));
		if(defaultShowItemId == null || defaultShowItemId.isEmpty()){
			log.info("defaultShowItemId is empty,please check db,promotionId:{}",promotionId);
			return null;
		}
		
		// 根据商品id 获取商品信息
		List<Merchandise> merchandises = merchandiseService.findMerchandiseByIds(defaultShowItemId);
		
		// 促销信息
		Promotion promotion = promotionService.findById(promotionId);
		if(promotion == null){
			throw new BusinessException("promotionId:"+promotion+",no promotion info.");
		}
		
		// 规则信息
		Rule rule = ruleService.findById(promotion.getRuleId());
		
		// 封装返回对象
		PromotionDTO promotionDTO = new PromotionDTO();
		promotionDTO.setPromotion(promotion);
		promotionDTO.setRule(rule);
		
		List<PromotionMerchandiseDTO> setPromotion = setPromotion(rule, merchandises);
		promotionDTO.setMerchandiseDTOs(setPromotion);
		
		return promotionDTO;
	}
	
	private List<PromotionMerchandiseDTO> setPromotion(Rule rule,List<Merchandise> merchandises){
	
		List<PromotionMerchandiseDTO> returnList = new LinkedList<PromotionMerchandiseDTO>();
		
		EnumPromotionType promotionType = EnumPromotionType.getPromotionType(rule.getRuleType());
		switch (promotionType) {
		case PRICE_DOWN:
			Double ruleStraightDownAmount = rule.getRuleStraightDownAmount();	// 直降金额
			
			for (Merchandise merchandise : merchandises) {
				
				PromotionMerchandiseDTO pmd = new PromotionMerchandiseDTO();
				pmd.setMerchandise(merchandise);
				
				Double merchandisePrice = merchandise.getMerchandisePrice();
				// 满足条件
				BigDecimal promotionPrice = new BigDecimal(merchandisePrice - ruleStraightDownAmount).setScale(2, BigDecimal.ROUND_HALF_UP);
				pmd.setPromotionPrice(promotionPrice);
				
				returnList.add(pmd);
			}
			
			break;
		default:
			
			for (Merchandise merchandise : merchandises) {
				PromotionMerchandiseDTO pmd = new PromotionMerchandiseDTO();
				pmd.setMerchandise(merchandise);
				returnList.add(pmd);
			}
			
			break;
		}
		
		return returnList;
	}

	private List<String> findItemId(List<PromotionMerchandiseMapping> list,List<Integer> isFirstShowFlg){
		
		List<String> returnList = new LinkedList<String>();
		
		for (PromotionMerchandiseMapping promotionMerchandiseMapping : list) {
			Integer isFirstShow = promotionMerchandiseMapping.getIsFirstShow();
			if(isFirstShowFlg.contains(isFirstShow)){
				returnList.add(promotionMerchandiseMapping.getMerchandiseId());
			}
		}
		
		return returnList;
	}
	
	public int save(PromotionMerchandiseMapping pmm) {
		return promotionMerchandiseMappingMapper.insertSelective(pmm);
	}

	
	public PromotionDTO findItemByPromotionId(String promotionId) {
		// 获取当前活动中的商品id
		List<PromotionMerchandiseMapping> list = promotionMerchandiseMappingMapper.selectByPromotionId(promotionId);
		if(list == null || list.isEmpty()){
			log.info("selectByPromotionId return null, please check db,promotionId:{}",promotionId);
			return null;
		}
		
		// 获取商品id
		List<String> ItemId = findItemId(list,Arrays.asList(CommonConstant.NUMBER_0,CommonConstant.NUMBER_1));
		if(ItemId == null || ItemId.isEmpty()){
			log.info("defaultShowItemId is empty,please check db,promotionId:{}",promotionId);
			return null;
		}
		
		// 根据商品id 获取商品信息
		List<Merchandise> merchandises = merchandiseService.findMerchandiseByIds(ItemId);
		
		// 促销信息
		Promotion promotion = promotionService.findById(promotionId);
		if(promotion == null){
			throw new BusinessException("promotionId:"+promotion+",no promotion info.");
		}
		
		// 规则信息
		Rule rule = ruleService.findById(promotion.getRuleId());
		
		// 封装返回对象
		PromotionDTO promotionDTO = new PromotionDTO();
		promotionDTO.setPromotion(promotion);
		promotionDTO.setRule(rule);
		
		List<PromotionMerchandiseDTO> setPromotion = setPromotion(rule, merchandises);
		promotionDTO.setMerchandiseDTOs(setPromotion);
		
		return promotionDTO;
	}

	/**
	 * 查询PromotionMerchandiseMapping 根据商品id 多个
	 */
	public List<PromotionMerchandiseMapping> findMappingByMerchandises(
			List<String> merchandiseIds) {
		return promotionMerchandiseMappingMapper.selectByMerchandiseIds(merchandiseIds);
	}
}
